/**
 * 
 */
package cen5501c.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Provides util methods that serve the cen5501c.common package.
 * 
 * @author Jiangyan Xu
 * 
 */
public class CommonUtil {
	// Uses Locale.UK to represents GMT TimeZone
	public static final SimpleDateFormat RFC822_DATE_GMT = new SimpleDateFormat(
			"EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.UK);

	public static final SimpleDateFormat CISE_DATE_GMT = new SimpleDateFormat(
			"dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.UK);

	public static String getDateAsRfc822String(Date date) {
		return CISE_DATE_GMT.format(date);
	}

	/**
	 * @throws ParseException
	 *             if the beginning of the specified string cannot be parsed.
	 */
	public static Date getDateFromRfc822String(String dateString)
			throws ParseException {
		if (dateString.startsWith("\"") && dateString.endsWith("\"")) {
			dateString = dateString.substring(1, dateString.length() - 1);
		}
		return CISE_DATE_GMT.parse(dateString);
	}
}
